package com.itheima.leyou.web;
import com.itheima.leyou.service.CategoryService;
import com.leyou.item.dto.CategoryDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {

 private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

//    @Autowired
//    private CategoryService categoryService;

    /**
     * 根据父类目id查询子类目的集合
     * @param pid
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> query(@RequestParam("pid")Long pid){
        return ResponseEntity.ok().body(categoryService.queryCategorysByPid(pid));
    }


    /**
     * 根据分类ids集合查询分类集合信息
     * @param ids
     * @return
     */
    //queryCategoryByIds
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok().body(categoryService.queryCategoryByIds(ids));
    }


    /**
     * 根据分类id查询分类信息
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){

        return ResponseEntity.ok().body(categoryService.queryCategoryById(id));
    }


    /**
     * 根据品牌id查询分类信息
     * @param bid
     * @return
     */
    @GetMapping("/of/brand")//http://api.leyou.com/item/category/of/brand/?id=2032
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id")Long bid){
        return ResponseEntity.ok(categoryService.queryCategoryByBrandId(bid));
    }
}
